package com.egoo.ticket.server.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.egoo.ticket.server.dao.HeadFieldMapper;
import com.egoo.ticket.server.pojo.dto.FieldInfo;
import com.egoo.ticket.server.pojo.dto.TableHeadFieldDto;
import com.egoo.ticket.server.pojo.entity.TableHeadField;
import com.egoo.ticket.server.pojo.vo.TableHeadFieldVo;
import com.egoo.ticket.server.service.HeadFieldService;
import com.egoo.ticket.server.utils.common.CommonUtil;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.CreateCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author donny
 * @since 2020-06-09
 */
@Slf4j
@Service
public class HeadFieldServiceImpl implements HeadFieldService {

	@Autowired
	HeadFieldMapper tableHeadFieldMapper;
	@Autowired
	CreateCodeUtil createCodeUtil;
	@Value("${server.project-sign}")
	private String projectSign;

	/**
	 * @Author: Donny_dong
	 * @Description: 添加表头信息
	 * @Param:
	 * @return:
	 * @Date: 2020/6/9
	 */
	@Override
	public Integer insert(TableHeadFieldDto tableHeadFieldDto) {
		List<FieldInfo> fieldInfoList = tableHeadFieldDto.getFieldInfoList();
		Long workOrderFilterMenuId = tableHeadFieldDto.getWorkOrderFilterMenuId();
		String tenantId = tableHeadFieldDto.getTenantId();
		ArrayList<TableHeadField> tableHeadFields = new ArrayList<>();
		Integer integer = null;
		for (FieldInfo fieldInfo : fieldInfoList) {
			TableHeadField tableHeadField = new TableHeadField();
			tableHeadField.setId(createCodeUtil.getTableHead());
			tableHeadField.setProp(fieldInfo.getProp());
			tableHeadField.setLabel(fieldInfo.getLabel());
			tableHeadField.setStatus(Integer.valueOf(ConstantCode.STATUS_NOMAL));
			tableHeadField.setWorkOrderFilterMenuId(workOrderFilterMenuId);
			tableHeadField.setTenantId(tenantId);
			tableHeadField.setType(fieldInfo.getType());
			tableHeadField.setOrder(fieldInfo.getOrder());
			tableHeadField.setBelongToTable(fieldInfo.getBelongToTable());
			if (fieldInfo.getSlot()) {
				tableHeadField.setSlot(ConstantCode.STATUS_NOMAL);
			} else {
				tableHeadField.setSlot(ConstantCode.STATUS_CLOSED);
			}
			tableHeadFields.add(tableHeadField);
		}
		tableHeadFieldMapper.insertEntity(tableHeadFields);
		return integer;
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 查询表头信息
	 * @Param:
	 * @return:
	 * @Date: 2020/6/10
	 */
	@Override
	public ArrayList<TableHeadFieldVo> select(TableHeadFieldDto tableHeadFieldDto) {
		ArrayList<TableHeadFieldVo> tableHeadFieldVos = new ArrayList<>();
		try {
			Long workOrderFilterMenuId = tableHeadFieldDto.getWorkOrderFilterMenuId();
			//如果是系统级别过滤器 直接取配置文件信息返回
			if (workOrderFilterMenuId == ConstantCode.SysFilter.ALL_FILTER_ID || workOrderFilterMenuId == ConstantCode.SysFilter.CREATE_FILTER_ID
					|| workOrderFilterMenuId == ConstantCode.SysFilter.EDIT_FILTER_ID || workOrderFilterMenuId == ConstantCode.SysFilter.MONITOR_FILTER_ID
					|| workOrderFilterMenuId == ConstantCode.SysFilter.WAIT_HANDLE_FILTER_ID || workOrderFilterMenuId == ConstantCode.SysFilter.FINISHED_FILTER_ID
					|| workOrderFilterMenuId == ConstantCode.SysFilter.FROM_DEPT_FILTER_ID) {
				//获取工单列表表头字段
				JSONArray jsonArray = CommonUtil.readConfig("/headfieldconfig/sysFilterHeadField-"+projectSign+".json");
				ArrayList<TableHeadFieldVo> sysHeadFieldVos = (ArrayList<TableHeadFieldVo>) JSON.parseArray(jsonArray.toJSONString(), TableHeadFieldVo.class);
				return sysHeadFieldVos;
			}
			//否则是自定过滤器 查询当前过滤器的所有需要查询回显的字段 work_order_head_field表 可控制是否启用
			List<TableHeadField> tableHeadFields = tableHeadFieldMapper.selectByFilterId(tableHeadFieldDto);
			for (TableHeadField tableHeadField : tableHeadFields
			) {
				TableHeadFieldVo tableHeadFieldVo = new TableHeadFieldVo();
				BeanUtils.copyProperties(tableHeadField, tableHeadFieldVo);
				if (ConstantCode.STATUS_NOMAL.equals(tableHeadField.getSlot())) {
					tableHeadFieldVo.setSlot(true);
				} else {
					tableHeadFieldVo.setSlot(false);
				}

				tableHeadFieldVos.add(tableHeadFieldVo);
			}
		} catch (Exception e) {
			log.info("表头信息查询失败" + e);

		}
		return tableHeadFieldVos;
	}


	/**
	 * @Author: Donny_dong
	 * @Description: 修改表头信息
	 * @Param:
	 * @return:
	 * @Date: 2020/6/10
	 */
	@Override
	public Integer update(TableHeadFieldDto tableHeadFieldDto) {
		Integer integer = tableHeadFieldMapper.deleteByFilterId(tableHeadFieldDto);
		if (integer < 0) {
			return integer;
		}
		return this.insert(tableHeadFieldDto);
	}
}
